package com.dearbinge.opentapi.content.userWallet;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.userWallet.UserWalletService;
import com.dearbinge.data.pojo.userWallet.UserWallet;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

@Api(tags = "c：钱包")
@RestController
@CrossOrigin
@RequestMapping("/userWallet")
@EnableAutoConfiguration
public class UserWalletController {

    @Autowired
    private UserWalletService userWalletService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody UserWallet userWallet){
        try {
            if (userWallet.getId() != null && userWallet.getId() != 0){
                Integer dataCode = userWalletService.update(userWallet);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                Integer dataCode = userWalletService.insert(userWallet);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<UserWallet> getById(Integer id){
        try {
            UserWallet userWallet = userWalletService.getById(id);
            return ResultMsg.ok(userWallet);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据用户ID查询
     */
    @ApiOperation(value = "根据用户ID查询",notes = "根据用户ID查询")
    @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query")
    @RequestMapping(value = "/getByUserId",method = RequestMethod.GET)
    public ResultMsg<UserWallet> getByUserId(String userId){
        try {
            UserWallet userWallet = userWalletService.getByUserId(userId);
            return ResultMsg.ok(userWallet);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID逻辑删除
     */
    @ApiOperation(value = "根据ID逻辑删除",notes = "根据ID逻辑删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = userWalletService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据条件分页查询
     */
    @ApiOperation(value = "根据条件分页查询",notes = "根据条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
            @ApiImplicitParam(name = "isDeleted",value = "是否删除（0：不删除  1：删除）不传默认条件为：0",paramType = "query"),
            @ApiImplicitParam(name = "pageNo",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "PageSize",value = "页面数量",paramType = "query")
    })
    @RequestMapping(value = "getByPage",method = RequestMethod.GET)
    public ResultMsg<Page> getByPage(String userId,Integer isDeleted,Integer pageNo,Integer pageSize){
        try {
            Page page = userWalletService.getByPage(userId,isDeleted,pageNo,pageSize);
            return ResultMsg.okData(page,"200");
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }
}
